<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="10">
        <el-col :span="3">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="姓名" prop="staffName">
            <el-input v-model="queryParams.staffName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="职别" prop="staffLevels">
            <el-select v-model="queryParams.staffLevels" clearable placeholder="请选择">
              <el-option v-for="item in categoryOptions" :key="item.jobCategoryId" :label="item.jobName"
                         :value="item.jobCategoryId"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="入岗时间">
            <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期"
                            type="daterange" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="手机号码" prop="staffPhone">
            <el-input v-model="queryParams.staffPhone" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="staffList" border>
      <el-table-column align="center" label="员工编号" prop="staffId" width="80px;"/>
      <el-table-column align="center" label="项目区域" prop="projectRegion" width="120px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName" width="200px;"/>
      <el-table-column align="center" label="员工姓名" prop="staffName" width="100px;"/>
      <el-table-column align="center" label="性别" prop="staffSex" width="80px;">
        <template slot-scope="scope">
          {{ formatGender(scope.row.staffSex) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="年龄" width="80px;">
        <template slot-scope="scope">
          {{ calculateAge(scope.row.staffIdcard) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="身份证号" prop="staffIdcard" width="200px;"/>
      <el-table-column align="center" label="联系方式" prop="staffPhone" width="150px;"/>
      <el-table-column align="center" label="组别" prop="groupName" width="80px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_groups" :value="scope.row.groupName"/>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="岗位编号" prop="projectPostNo" width="120px;"/>-->
      <el-table-column align="center" label="岗位" prop="projectPostName" width="100px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="职务" prop="projectDuitsName" width="100px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="职别" prop="jobName" width="100px;"/>
      <el-table-column align="center" label="银行卡号" prop="bankCard" width="200px;">
        <template slot-scope="scope">
          {{ scope.row.bankCard ? scope.row.bankCard.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ') : '' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险单号" prop="insuranceOdd" width="200px;"/>
      <el-table-column align="center" label="状态" prop="staffStatus"width="80px;">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.staffStatus == 2 && (scope.row.resignApprovalStatus != 3)" type="success">在岗</el-tag>
          <el-tag v-if="[3].includes(scope.row.resignApprovalStatus)" type="danger">待离职</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="家庭住址" prop="staffAddress"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="100px;">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:staff:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-dropdown v-hasPermi="['cld:staff:resign', 'cld:staff:invalid', 'cld:staff:edit', 'cld:staff:resume']" size="mini"
                       @command="(command) => handleCommand(command, scope.row)">
            <el-button icon="el-icon-d-arrow-right" size="mini" type="text">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPermi="['cld:staff:edit']" command="handleResetPassword"
                                >重置密码
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['cld:staff:resume']" command="handleResume"
                               >履历表
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['cld:staff:resign']" command="handleResignation"
                               >离职
              </el-dropdown-item>

            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 修改人员 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="open" append-to-body title="个人信息录入"
               width="50%">
      <add-staff-vue v-if="open" ref="addStaff" :staffId="staffId" @success="handleSubmitSuccess"></add-staff-vue>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStaff">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 离职 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="resigOpen" append-to-body title="离职"
               width="500px">
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row :gutter="20">
          <el-form-item :rules="{
            required: true, message: '离职原因不能为空', trigger: 'blur'
          }" label="离职原因" prop="resignReason">
            <el-input v-model="form.resignReason" :rows="4" placeholder="请输入" type="textarea"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="20">
          <el-form-item :rules="{
            required: true, message: '离职时间不能为空', trigger: 'blur'
          }" label="离职时间" prop="resignTime">
            <el-date-picker v-model="form.resignTime" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 100%;"
                            type="date"></el-date-picker>
          </el-form-item>
        </el-row>
        <el-row :gutter="20">
          <el-form-item :rules="{
            required: true, message: '离职申请表不能为空', trigger: 'blur'
          }" label="离职申请表" prop="resignTableImg">
            <file-upload v-model="form.resignTableImg" @input="handleFileInput"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitResignation">确 定</el-button>
        <el-button @click="resigOpen = false">取 消</el-button>
      </div>
    </el-dialog>

     <!-- 履历表查看 -->
     <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="resumeOpen" append-to-body title="履历表"
               width="900px">
               <div id="printLLBForm">
        <staff-resume-view :staffId="staffId"> </staff-resume-view>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button v-print="'#printLLBForm'" type="primary"
          >打印</el-button
        >
        <el-button @click="resumeOpen = false">关 闭</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import {delStaff, getStaff, listStaff, resignStaff,resetPasswordStaff} from "@/api/cld/staff";
import {getListCategory} from '@/api/cld/category'
import addStaffVue from "./addStaff.vue";
import staffResumeView from './staffResumeView.vue';
export default {
  name: "Staff",
  dicts: ['cld_staff_levels', 'cld_project_post', 'cld_project_region', 'cld_project_groups', 'cld_project_duits'],
  inject: ['formatGender'],
  components: {
    addStaffVue,
    staffResumeView
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员花名册表格数据
      staffList: [],
      // 是否显示弹出层
      open: false,
      resumeOpen: false,
      resumeImg: '',
      resigOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        staffName: null,
        staffStatus: 2,
        isPrepared: 0,
        staffPhone: null
      },
      // 表单参数
      form: {},
      dateRange: [],
      staffId: null,
      // 职别列表
      categoryOptions: []
    };
  },
  created() {
    this.getList();
    // 获取职别列表
    this.handleGetListCategory();
  },
  methods: {
    /** 查询人员花名册列表 */
    getList() {
      this.loading = true;
      listStaff(this.addDateRange(this.queryParams, this.dateRange, 'PostTime')).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 职别列表
    handleGetListCategory() {
      getListCategory().then(res => {
        this.categoryOptions = res.data;
      })
    },
    // 修改员工
    handleUpdate(row) {
      this.staffId = row.staffId;
      this.open = true;
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 提交
    submitStaff() {
      this.$refs.addStaff.submitForm();
    },
    // 提交回调
    handleSubmitSuccess() {
      this.open = false;
      this.getList();
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResignation":
          this.handleResignation(row);
          break;
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleResume":
          this.handleResume(row);
          break;
        case "handleResetPassword":
          this.handleResetPassword(row);
          break;
        default:
          break;
      }
    },
    // 离职
    handleResignation(row) {
      this.reset();
      getStaff(row.staffId).then(res => {
        this.form = res.data;
        this.resigOpen = true;
      })
    },
    // 离职提交
    submitResignation() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          resignStaff(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.resigOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除员工姓名为"' + row.staffName + '"的数据项？').then(function () {
        return delStaff(row.staffId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 履历表查看
    handleResume(row){
      this.resumeOpen = true;
      this.staffId = row.staffId;
    },
    handleFileInput(e) {
      this.form.resignTableImg = e;
    },
     // 重置密码
     handleResetPassword(row) {
      this.$modal.confirm('是否重置密码为[123456]？').then(function () {
        return resetPasswordStaff(row.staffId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => { });
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
.el-image{
  width: 100%;
}
</style>
